﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FactoryControlStore
{
    public partial class LineBodyCtrl : UserControl
    {

        Timer timer;
        public LineBodyCtrl()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.Size = new Size(120, 100);
            lineWidth = Height / 10;
            timer = new Timer();
            timer.Interval = 300;
            timer.Tick += Timer_Tick;
            timer.Enabled = false;
        }


        private int count { get; set; } = 0;
        public int Count { get { return count; } set { count = value; Refresh(); } }

        private void Timer_Tick(object sender, EventArgs e)
        {
            count++;
            if (count % 2 == 0)
                lineWidth = Height / 10;
            else
                lineWidth = 0;
            if (count == 20000)
            {
                count = 0;

            }
            Refresh();
        }


        private int lineWidth;
        [Description("箭头大小方向"), Category("自定义")]
        public int LineWidth
        {
            get { return lineWidth; }
            set
            {
                lineWidth = value;
                Refresh();
            }
        }



        /// <summary>
        /// 线体左半侧颜色
        /// </summary>
        private Color lineBodyLeft = Color.FromArgb(64, 64, 64);

        /// <summary>
        /// 线体左半侧颜色
        /// </summary>
        [Description("线体颜色"), Category("自定义")]
        public Color LineBodyLeft
        {
            get { return lineBodyLeft; }
            set
            {
                lineBodyLeft = value;
                Refresh();
            }
        }


        /// <summary>
        /// 线体右半侧颜色
        /// </summary>
        private Color lineBodyRight = Color.Teal;

        /// <summary>
        /// 线体右半侧颜色
        /// </summary>
        [Description("线体颜色"), Category("自定义")]
        public Color LineBodyRight
        {
            get { return lineBodyRight; }
            set
            {
                lineBodyRight = value;
                Refresh();
            }
        }

        /// <summary>
        /// 线体右半侧颜色
        /// </summary>
        private Color arrowColor = Color.LightGoldenrodYellow;

        /// <summary>
        /// 线体右半侧颜色
        /// </summary>
        [Description("箭头颜色"), Category("自定义")]
        public Color ArrowColor
        {
            get { return arrowColor; }
            set
            {
                arrowColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// 是否有料
        /// </summary>
        private bool isMaterial = false;

        /// <summary>
        /// 是否有料
        /// </向上移动>
        [Description("是否有料"), Category("自定义")]
        public bool IsMaterial
        {
            get { return isMaterial; }
            set
            {
                isMaterial = value;
                Refresh();
            }
        }



        /// <summary>
        /// 方位
        /// </summary>
        private ArrowDirection arrowDirection = ArrowDirection.Left;

        /// <summary>
        /// 方位
        /// </summary>
        [Description("箭头方向"), Category("自定义")]
        public ArrowDirection ArrowDirection
        {
            get { return arrowDirection; }
            set
            {
                arrowDirection = value;
                Refresh();
            }
        }



        /// <summary>
        /// 方位
        /// </summary>
        private LineDirection lineDirection = LineDirection.Across;

        /// <summary>
        /// 方位
        /// </summary>
        [Description("线体方向"), Category("自定义")]
        public LineDirection LineDirection
        {
            get { return lineDirection; }
            set
            {
                lineDirection = value;
                Refresh();
            }
        }




        protected override void OnPaint(PaintEventArgs e)
        {
            var g = e.Graphics;
            Rectangle rectangleUp = Rectangle.Empty;
            Rectangle rectangleDown = Rectangle.Empty;
            Rectangle rectangleLeft = Rectangle.Empty;
            Rectangle rectangleRight = Rectangle.Empty;
            Rectangle rectangleCentert = Rectangle.Empty;
            switch (lineDirection)
            {
                case LineDirection.Across:
                    rectangleUp = new Rectangle(0, 0, Width, Height / 5);
                    rectangleDown = new Rectangle(0, Height - Height * 1 / 5, Width, Height / 5);
                    rectangleLeft = new Rectangle(Width / 10, 0, Height / 5, Height);
                    rectangleRight = new Rectangle(Width - Height / 5 - Width / 10, 0, Height / 5, Height);
                    rectangleCentert = new Rectangle(Width / 2 - Height / 10, 0, Height / 5, Height);

                    //p.Dispose();
                    LinearGradientBrush mylineargradientbrushAcross = new LinearGradientBrush(rectangleUp, LineBodyLeft, LineBodyRight, LinearGradientMode.ForwardDiagonal);
                    LinearGradientBrush mylineargradientbrushAcross1 = new LinearGradientBrush(rectangleDown, LineBodyLeft, LineBodyRight, LinearGradientMode.ForwardDiagonal);
                    LinearGradientBrush mylineargradientbrushAcross2 = new LinearGradientBrush(rectangleLeft, LineBodyLeft, LineBodyRight, LinearGradientMode.ForwardDiagonal);
                    LinearGradientBrush mylineargradientbrushAcross3 = new LinearGradientBrush(rectangleRight, LineBodyLeft, LineBodyRight, LinearGradientMode.ForwardDiagonal);
                    LinearGradientBrush mylineargradientbrushAcross4 = new LinearGradientBrush(rectangleCentert, LineBodyLeft, LineBodyRight, LinearGradientMode.ForwardDiagonal);
                    g.SetGDIHigh();
                    g.FillRectangle(mylineargradientbrushAcross, 0, 0, Width, Height / 5);
                    g.FillRectangle(mylineargradientbrushAcross1, 0, Height - Height * 1 / 5, Width, Height / 5);
                    g.FillRectangle(mylineargradientbrushAcross2, Width / 10, 0, Height / 5, Height);
                    g.FillRectangle(mylineargradientbrushAcross3, Width - Height / 5 - Width / 10, 0, Height / 5, Height);
                    g.FillRectangle(mylineargradientbrushAcross4, Width / 2 - Height / 10, 0, Height / 5, Height);

                    if (isMaterial && lineWidth >= 0)
                    {
                        timer.Enabled = true;
                        switch (arrowDirection)
                        {
                            case ArrowDirection.Left:
                                if (count % 2 == 0)
                                {
                                    Pen pAcrossLeft = new Pen(arrowColor, lineWidth/*Height / 3*/);

                                    pAcrossLeft.DashStyle = DashStyle.Solid;//恢复实线
                                    pAcrossLeft.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头

                                    g.DrawLine(pAcrossLeft, Width - Width / 10, Height / 10, Width / 10, Height / 10);
                                    g.DrawLine(pAcrossLeft, Width - Width / 10, Height - Height / 10, Width / 10, Height - Height / 10);
                                }


                                break;
                            case ArrowDirection.Right:
                                if (count % 2 == 0)
                                {
                                    Pen pAcrossRight = new Pen(arrowColor, lineWidth);

                                    pAcrossRight.DashStyle = DashStyle.Solid;//恢复实线
                                    pAcrossRight.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头
                                    g.DrawLine(pAcrossRight, Width / 10, Height / 10, Width - Width / 10, Height / 10);
                                    g.DrawLine(pAcrossRight, Width / 10, Height - Height / 10, Width - Width / 10, Height - Height / 10);
                                }
                                break;

                            default:
                                break;
                        }

                    }
                    else
                    {
                        timer.Enabled = false;
                    }


                    break;
                case LineDirection.Vertical:
                    rectangleUp = new Rectangle(0, 0, Width / 5, Height);
                    rectangleDown = new Rectangle(Width - Width * 1 / 5, 0, Width / 5, Height);
                    rectangleLeft = new Rectangle(0, Height / 10, Width, Width / 5);
                    rectangleRight = new Rectangle(0, Height - Width / 5 - Height / 10, Width, Width / 5);
                    rectangleCentert = new Rectangle(0, Height / 2 - Width / 10, Width, Width / 5);

                    LinearGradientBrush mylineargradientbrushVertical = new LinearGradientBrush(rectangleUp, LineBodyLeft, LineBodyRight, LinearGradientMode.ForwardDiagonal);
                    LinearGradientBrush mylineargradientbrushVertical1 = new LinearGradientBrush(rectangleDown, LineBodyLeft, LineBodyRight, LinearGradientMode.ForwardDiagonal);
                    LinearGradientBrush mylineargradientbrushVertical2 = new LinearGradientBrush(rectangleLeft, LineBodyLeft, LineBodyRight, LinearGradientMode.ForwardDiagonal);
                    LinearGradientBrush mylineargradientbrushVertical3 = new LinearGradientBrush(rectangleRight, LineBodyLeft, LineBodyRight, LinearGradientMode.ForwardDiagonal);
                    LinearGradientBrush mylineargradientbrushVertical4 = new LinearGradientBrush(rectangleCentert, LineBodyLeft, LineBodyRight, LinearGradientMode.ForwardDiagonal);
                    g.SetGDIHigh();
                    g.FillRectangle(mylineargradientbrushVertical, 0, 0, Width / 5, Height);
                    g.FillRectangle(mylineargradientbrushVertical1, Width - Width * 1 / 5, 0, Width / 5, Height);
                    g.FillRectangle(mylineargradientbrushVertical2, 0, Height / 10, Width, Width / 5);
                    g.FillRectangle(mylineargradientbrushVertical3, 0, Height - Width / 5 - Height / 10, Width, Width / 5);
                    g.FillRectangle(mylineargradientbrushVertical4, 0, Height / 2 - Width / 10, Width, Width / 5);



                    if (isMaterial)
                    {
                        timer.Enabled = true;

                        switch (arrowDirection)
                        {
                            case ArrowDirection.Up:
                                if (count % 2 == 0)
                                {
                                    Pen pVerticalUp = new Pen(arrowColor, lineWidth);
                                    pVerticalUp.DashStyle = DashStyle.Solid;//恢复实线
                                    pVerticalUp.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头
                                    g.DrawLine(pVerticalUp, Width / 10, Height - Height / 10, Width / 10, Height / 10);//通过起点终点改变箭头方向
                                    g.DrawLine(pVerticalUp, Width - Width / 10, Height - Height / 10, Width - Width / 10, Height / 10);//通过起点终点改变箭头方向
                                }
                                break;
                            case ArrowDirection.Down:
                                if (count % 2 == 0)
                                {
                                    Pen pVerticalDown = new Pen(arrowColor, lineWidth);
                                    pVerticalDown.DashStyle = DashStyle.Solid;//恢复实线
                                    pVerticalDown.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头
                                    g.DrawLine(pVerticalDown, Width / 10, Height / 10, Width / 10, Height - Height / 10);//通过起点终点改变箭头方向
                                    g.DrawLine(pVerticalDown, Width - Width / 10, Height / 10, Width - Width / 10, Height - Height / 10);//通过起点终点改变箭头方向
                                }
                                break;
                            case ArrowDirection.None:
                                break;
                            default:
                                break;
                        }

                    }
                    else
                    {
                        timer.Enabled = false;
                    }

                    break;
            }



        }



        //双击事件
        private void LineBodyThree_DoubleClick(object sender, EventArgs e)
        {
        }
    }


    /// <summary>
    /// 箭头枚举
    /// </summary>
    public enum ArrowDirection
    {
        /// <summary>
        /// Left
        /// </summary>
        Left,
        /// <summary>
        /// Right
        /// </summary>
        Right,
        /// <summary>
        /// Up
        /// </summary>
        Up,
        /// <summary>
        /// Down
        /// </summary>
        Down,
        /// <summary>
        /// None
        /// </summary>
        None
    }

    /// <summary>
    /// 线体方位枚举
    /// </summary>
    public enum LineDirection
    {
        /// <summary>
        /// Across
        /// </summary>
        Across,
        /// <summary>
        /// vertical
        /// </summary>
        Vertical
    }
}